package com.slfx.qrcode.rest;

import com.slfx.qrcode.util.QRCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author slfx
 */
@RestController
@RequestMapping("/qr")
@Slf4j
public class QrController {

    @GetMapping(value = "/generate")
    public void generateQR(@RequestParam("content")String content, HttpServletResponse response) {
        BufferedImage image;
        // 禁止图像缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        image = QRCodeUtil.createImage(content);
        // 创建二进制的输出流
        try(ServletOutputStream sos = response.getOutputStream()){
            // 将图像输出到Servlet输出流中。
            ImageIO.write(image, "jpeg", sos);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping(value = "/generateWithLogo")
    public void generateWithLogo(@RequestParam("content")String content, HttpServletResponse response) {
        BufferedImage image;
        // 禁止图像缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        image = QRCodeUtil.createImage(content);
        InputStream resourceAsStream = this.getClass().getResourceAsStream("/static/1.jpg");
        //插入图片
        QRCodeUtil.insertImage(image,resourceAsStream,true);
        // 创建二进制的输出流
        try(ServletOutputStream sos = response.getOutputStream()){
            // 将图像输出到Servlet输出流中。
            ImageIO.write(image, "jpeg", sos);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping(value = "/generateWithWordTopAndDown")
    public void generateWithWordTopAndDown(@RequestParam("content")String content,@RequestParam("topText")String topText,@RequestParam("downText")String downText, HttpServletResponse response) {
        BufferedImage image;
        // 禁止图像缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        image = QRCodeUtil.createImage(content);
        BufferedImage bufferedImage = QRCodeUtil.addNote(image, topText,downText);
        // 创建二进制的输出流
        try(ServletOutputStream sos = response.getOutputStream()){
            // 将图像输出到Servlet输出流中。
            ImageIO.write(bufferedImage, "jpeg", sos);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping(value = "/generateWithWordLeft")
    public void generateWithWordLeft(@RequestParam("content")String content,@RequestParam("leftText")String leftText,HttpServletResponse response) {
        BufferedImage image;
        // 禁止图像缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        image = QRCodeUtil.createImage(content);
        BufferedImage bufferedImage = QRCodeUtil.addNote(image, leftText);
        // 创建二进制的输出流
        try(ServletOutputStream sos = response.getOutputStream()){
            // 将图像输出到Servlet输出流中。
            ImageIO.write(bufferedImage, "jpeg", sos);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}